package com.eschool.beans;

import java.util.Collections;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.primefaces.model.DualListModel;
import org.primefaces.model.UploadedFile;

import com.eschool.beans.converter.UserConverter;
import com.eschool.model.User;
import com.eschool.service.AlertService;
import com.eschool.service.impl.AlertServiceImpl;

@ManagedBean(name="alertBean")
@ViewScoped
public class AlertBean {
	
	String message;
	String subject;
	private DualListModel<User> reciepients;
	private UploadedFile file;

	public UploadedFile getFile() {
		return file;
	}

	public void setFile(UploadedFile file) {
		this.file = file;
	}

	public AlertBean() {
		reciepients = new DualListModel<User>(UserConverter.users, Collections.EMPTY_LIST);
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public DualListModel<User> getReciepients() {
		return reciepients;
	}

	public void setReciepients(DualListModel<User> reciepients) {
		this.reciepients = reciepients;
	}

	AlertService service = new AlertServiceImpl();
	
	public String action()
	{
		/*for (Iterator iterator = reciepients.getTarget().iterator(); iterator.hasNext();) {
			User user = (User) iterator.next();
			System.out.println(user.getEmail());
			
		}*/
		
		if(service.sendAlert(subject, message, reciepients.getTarget(), null))
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
					FacesMessage.SEVERITY_INFO,null, "Alert Succesfully Sent"));
		else
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
					FacesMessage.SEVERITY_INFO,null, "Alert Not Sent"));
		
		return "SendAlert";
	}

}
